package com.pig.controller.user;


import com.alibaba.dubbo.config.annotation.Reference;
import com.pig.pojo.user.User;
import com.pig.service.UserService;
import com.pig.service.order.DubboOrderService;
import com.pig.util.SysResult;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class UserController {

    //使用常量定义cookie名称.
    private static final String PAGE_TICKET = "PAGE_TICKET";

    @Autowired
    private UserService userService;
    @Reference(check = false)
    private DubboOrderService orderService;

    /*
     * 登录
     * */
    @RequestMapping("doLogin")
    @ResponseBody
    public SysResult doLogin(String username, String password, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(username + password);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken();
        token.setUsername(username);
        token.setPassword(password.toCharArray());
        System.out.println("@@@ >> " + token);
        subject.login(token);//提交给securityManager

        User user1 = (User) SecurityUtils.getSubject().getPrincipal();
        System.out.println("!!! "+ user1);
        HttpSession session = request.getSession(true);
        session.setMaxInactiveInterval(1000);
        session.setAttribute("globalUser",user1);

        //需要将数据保存到cookie中
        Cookie cookie = new Cookie("PAGE_TICKET", username);
        cookie.setPath("/");
        cookie.setMaxAge(7 * 24 * 60 * 60); //设定有效期 7天有效 单位秒
        //cookie.setDomain("page.com");   //主要域名中由jt.com则可以共享数据
        response.addCookie(cookie);   //将数据写入客户端
        return SysResult.success();
    }

    /*
     * 退出
     * */
    @RequestMapping("/page/logout")
    public String logout(String username, HttpSession session, HttpServletResponse response) {
        Subject subject = SecurityUtils.getSubject();
        Cookie cookie = new Cookie("PAGE_TICKET", username);
        cookie.setPath("/");
        cookie.setMaxAge(0); //立即删除
        //cookie.setDomain("page.com");   //主要域名中由jt.com则可以共享数据
        response.addCookie(cookie);   //将数据写入客户端
        subject.logout();
        return "redirect:/page/index.html";
    }

    /**
     * 需要:完成用户注册入库操作
     * url:/user/doSignUp
     * 返回值:status==200
     */
    @RequestMapping("/doSignUp")
    @ResponseBody
    public SysResult doSignUp(User user) {
        user.setCreatedTime(new Date());
        user.setModifiedTime(new Date());
        userService.doSaveUser(user);
        Long userId = user.getId();
        orderService.insertAddr(userId);
        return SysResult.success();
    }


    /**
     * 需求:检查用户注册名字是否可用
     * url: /user/checkUsername
     */
    @RequestMapping("/checkUsername")
    @ResponseBody
    public SysResult checkUsername(String username) {
        int count = userService.checkUsername(username);
        if (count > 0) {
            return SysResult.fail();
        }
        return SysResult.success();
    }


    /**
     * 需求:检查用户注册手机号是否可用
     * url: /user/checkPhone
     */
    @RequestMapping("/checkPhone")
    @ResponseBody
    public SysResult checkPhone(String phone) {
        int count = userService.checkPhone(phone);
        if (count > 0) {
            return SysResult.fail();
        }
        return SysResult.success();
    }
}
